﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using DomainModel.Entities;
using System.Data.Linq;

namespace DomainModel.Concrete
{
    // Implementation of the Jobs section of the database.
    // The middle layer of the DAL.
    class SqlJobsRepository : IJobsRepository
    {
        private Table<Job> jobsTable;

        public SqlJobsRepository(string connectionString)
        {
            jobsTable = (new DataContext(connectionString)).GetTable<Job>();
        }

        public IQueryable<Job> Jobs
        {
            get { return jobsTable; }
        }

        public void SaveJob(Job job)
        {
            if (job.JobID == 0)
            {
                jobsTable.InsertOnSubmit(job);
            }
            else
            {
                jobsTable.Attach(job);
                jobsTable.Context.Refresh(RefreshMode.KeepCurrentValues, job);
            }

            jobsTable.Context.SubmitChanges();
        }

        public void DeleteJob(Job job)
        {
            jobsTable.DeleteOnSubmit(job);
            jobsTable.Context.SubmitChanges();
        }
    }
}
